import {createRouter, createWebHistory} from 'vue-router'
import {ElMessage} from "element-plus";
import { isAuthenticated, getUserInfo, clearAuth } from '@/utils/auth'
const Login = () => import('@/views/Login/Login.vue')
const Register = () => import('@/views/Register/Register.vue')
const Home = () => import('@/views/Home/Home.vue')
const HomePage = () => import('@/views/Home/HomePage.vue')
const UserProfile = () => import('@/views/UserManagement/UserProfile.vue')
const ProjectList = () => import('@/views/Project/ProjectList.vue')
const CaseList = () => import('@/views/Case/CaseList.vue')
const SuiteList = () => import('@/views/Suite/SuiteList.vue')
const LogList = () => import('@/views/Log/LogList.vue')
const AdminUserManagement = () => import('@/views/UserManagement/AdminUserManagement.vue')
const DepartmentManagement = () => import('@/views/Department/DepartmentManagement.vue')
const EnvironmentManagement = () => import('@/views/Project/EnvironmentManagement.vue')

const routes = [
    {
        path: '/login',
        name: 'login',
        component: Login,
    },
    {
        path: '/register',
        name: 'register',
        component: Register
    },
    {
        path: '/',
        component: Home,
        redirect: '/home', // 首页默认重定向到首页仪表盘
        children: [
            {
                path: 'home',
                name: 'home',
                component: HomePage,
                meta: {
                    isLoggedIn: true,
                    title: '首页仪表盘'
                }
            },
            {
                path: 'profile',
                name: 'profile',
                component: UserProfile,
                meta: {
                    isLoggedIn: true,
                    title: '个人中心'
                }
            },
            {
                path: 'projects',
                name: 'projects',
                component: ProjectList,
                meta: {
                    isLoggedIn: true,
                    title: '项目列表'
                }
            },
            {
                path: 'project/:id/environments',
                name: 'project-environments',
                component: EnvironmentManagement,
                meta: {
                    isLoggedIn: true,
                    title: '环境配置管理'
                }
            },
            {
                path: 'cases',
                name: 'cases',
                component: CaseList,
                meta: {
                    isLoggedIn: true,
                    title: '测试用例'
                }
            },
            {
                path: 'suites',
                name: 'suites',
                component: SuiteList,
                meta: {
                    isLoggedIn: true,
                    title: '测试套件'
                }
            },
            {
                path: 'logs',
                name: 'logs',
                component: LogList,
                meta: {
                    isLoggedIn: true,
                    title: '日志管理'
                }
            },
            {
                path: 'admin/users',
                name: 'admin-users',
                component: AdminUserManagement,
                meta: {
                    isLoggedIn: true,
                    title: '用户管理',
                    requiresAdmin: true
                }
            },
            {
                path: 'admin/departments',
                name: 'admin-departments',
                component: DepartmentManagement,
                meta: {
                    isLoggedIn: true,
                    title: '部门管理',
                    requiresAdmin: true
                }
            }
        ]
    },
]

const router = createRouter({
    history: createWebHistory(),
    routes
})

router.beforeEach((to, from, next) => {
    if (to.meta.isLoggedIn) {
        // 检查是否已登录
        if (!isAuthenticated()) {
            clearAuth("请先登录！")
            next('/login')
            return
        }
        
        // 获取用户信息
        const userInfo = getUserInfo()
        if (!userInfo) {
            clearAuth("用户信息无效，请重新登录！")
            next('/login')
            return
        }
        
        // 检查管理员权限
        if (to.meta.requiresAdmin) {
            const userType = userInfo.user_type
            if (userType !== 'ADMIN' && userType !== 'MANAGER') {
                ElMessage.error("您没有权限访问此页面！")
                next('/home')
                return
            }
        }
        
        next()
    } else {
        // 如果用户已登录但访问登录页，重定向到首页
        if ((to.path === '/login' || to.path === '/register') && isAuthenticated()) {
            next('/home')
            return
        }
        next()
    }
})

export default router